﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace FrameworkLib.Configuration
{
    #region Summary...
    /// <summary>
    /// Generic class for accessing to a Configuration Section (in app.config or web.config)...
    /// </summary>
    /// <typeparam name="T">the type of section to access to (this type have to be inherited from ConfigurationSection)</typeparam>
    /// <remarks>
    /// 
    /// Example of Use:
    ///
    ///   In this example we try to get the informations of SessionManager configuration section from app.config.
    ///   
    ///   In the app.config the section is configured like this:
    ///   
    ///   <ConfigurationGroup>
    ///    <SessionManager>
    ///      <SessionItem key="AuthenticationFacadeSingle" index="10" />   <-- this is a single SessionItem element...
    ///      <SessionItems>                                                <-- this is a collection of SessionItem elements... 
    ///        <add key="AuthenticationFacade" index="0" />
    ///        <add key="CurrentUser" index="1" />
    ///        <add key="OtherSample" index="2" />
    ///      </SessionItems>
    ///    </SessionManager>
    ///  </ConfigurationGroup>
    /// 
    ///  in the .config file we have to add this for managing the custom section throught the configuration API:
    ///    <configSections>
    ///      <sectionGroup name="ConfigurationGroup">
    ///        <section name="SessionManager" type="FLib.ConfigurationSections.EnumConfigurationSection.SessionManagerSection, FLib" 
    ///                 allowLocation="true" 
    ///                 allowDefinition="Everywhere"  />
    ///      </sectionGroup>
    ///    </configSections>
    /// 
    ///  the in code, we have to specify the path for accessing to the configuration section we want to read:  
    ///  
    ///  using FLib.ConfigurationSections;
    ///  using FLib.ConfigurationSections.SessionManager;
    ///   
    ///  ConfigurationSectionManager<SessionManagerSection> mm = 
    ///                      new ConfigurationSectionManager<SessionManagerSection>("ConfigurationGroup/SessionManager");
    /// 
    ///  string key = mm.Section.SessionItem.Key;     <-- we can read a property of a single SessionItem element
    ///  FLib.ConfigurationSections.SessionManager.SessionItemCollection coll = mm.Section.SessionItems   <-- we read the collection of item found in the "SessionItems" key...
    /// 
    /// 
    /// </remarks>
    #endregion
    public class FLibConfigurationSectionManager<T> where T : FLibConfigurationSection<T>
    {
        private T m_section = null;

        public T Section
        {
            get { return m_section; }
        }

        public FLibConfigurationSectionManager(string sectionName)
        {
            System.Configuration.Configuration c = ConfigurationManager.OpenExeConfiguration("");
            m_section = (T)c.GetSection(sectionName);
        }

        public FLibConfigurationSectionManager(string path, string sectionName)
        {
            System.Configuration.Configuration c = ConfigurationManager.OpenExeConfiguration(path);
            m_section = (T)c.GetSection(sectionName);
        }

    }
}
